package com.nercel.dsj.gksales.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Strings;
import com.nercel.dsj.gksales.exception.ResponseCode;
import com.nercel.dsj.gksales.model.ListResult;
import com.nercel.dsj.gksales.model.QueryParams;
import com.nercel.dsj.gksales.model.ResponseResult;
import com.nercel.dsj.gksales.model.entity.Spike;
import com.nercel.dsj.gksales.service.SpikeService;
import com.nercel.dsj.gksales.util.ApiResultUtil;
import com.nercel.dsj.gksales.util.SpikeResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;

@RestController
@Api(tags = "秒杀活动")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SpikeApiController extends BaseApiController {

	@Autowired
	private SpikeService spikeService;

	@GetMapping(value = "/api/v1/spike")
	@ApiOperation(value = "根据id查询秒杀活动")
	@ResponseBody
	public ResponseResult querySpikeById(@RequestParam(value = "id", required = true) Integer id) {
		return ApiResultUtil.success(spikeService.findById(id));
	}

	@GetMapping(value = "/api/v1/spikes")
	@ApiOperation(value = "秒杀活动列表查询")
	@ResponseBody
	public ListResult<Spike> querySpike(@ApiParam(hidden = true) QueryParams queryParams,
			@ApiParam(value = "页码") @RequestParam(required = false) Integer page,
			@ApiParam(value = "单页数量") @RequestParam(required = false) Integer pageSize,
			@ApiParam(value = "省编码") @RequestParam(required = false) String proviceCode,
			@ApiParam(value = "活动开始时间大于") @RequestParam(required = false) String startTime,
			@ApiParam(value = "活动结束时间小于") @RequestParam(required = false) String endTime,
			@ApiParam(value = "限制人数") @RequestParam(required = false) Integer limitNumber) {
		queryParams.put("page", page);
		queryParams.put("pageSize", pageSize);
		if (!Strings.isNullOrEmpty(proviceCode)) {
			queryParams.put("proviceCode", proviceCode);
		}
		if (!Strings.isNullOrEmpty(startTime)) {
			queryParams.put("startTime", startTime);
		}
		if (!Strings.isNullOrEmpty(endTime)) {
			queryParams.put("endTime", endTime);
		}
		if (limitNumber != null) {
			queryParams.put("limitNumber", limitNumber);
		}
		return spikeService.listWithPaging(queryParams);
	}

	@PostMapping(value = "/api/v1/spike")
	@ApiOperation(value = "创建秒杀活动（返回活动id）")
	@ResponseBody
	public ResponseResult spike(@RequestBody Spike spike) {
		spike.setRemainNumber(spike.getLimitNumber());// 初始化时剩余数量就是限制数量
		spikeService.save(spike);
		return ApiResultUtil.success(spike.getId());
	}

	@PostMapping(value = "/api/v1/doSpike")
	@ApiOperation(value = "秒杀")
	@ResponseBody
	public ResponseResult doSpike(@ApiParam(value = "活动id") @RequestParam(required = true) Integer id,
			@ApiParam(value = "参加活动人员的手机号") @RequestParam(required = true) String phone) {
		Spike spike = spikeService.findById(id);
		if (spike != null) {
			int result = spikeService.spike(spike, phone);
			if (result == SpikeResult.SUCCESS.getCode()) {
				return ApiResultUtil.success(SpikeResult.SUCCESS.getMsg(), null);
			} else {
				return new ResponseResult(ResponseCode.INTERNAL_SERVER_ERROR.getCode(),
						SpikeResult.getMsgByCode(result), null);
			}
		} else {
			return new ResponseResult(ResponseCode.INTERNAL_SERVER_ERROR.getCode(), SpikeResult.FAILNOTEXIST.getMsg(),
					null);
		}
	}

	@DeleteMapping(value = "/api/v1/spike")
	@ApiOperation(value = "根据id删除秒杀活动")
	@ResponseBody
	public ResponseResult deleteSpikeById(@RequestParam(value = "id", required = true) Integer id) {
		spikeService.delete(id);
		return ApiResultUtil.success();
	}
}
